import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';

export function useAuth() {
    const router = useRouter();

    const logout = () => {
        sessionStorage.removeItem('token')
        sessionStorage.removeItem('auth')
        sessionStorage.removeItem('username')
        sessionStorage.removeItem('user_id')
        sessionStorage.removeItem('tokenExpiry')
        ElMessage.warning('登录超时，请重新登录')
        router.replace('/login')
        return Promise.reject(new Error('Token expired.'));
    };

    return { logout };
}
